#Region "Comments"
'  Module                       :    Validation Class
'  Purpose                      :    To validate the values in textboxes
#End Region
Imports System.Text.RegularExpressions
Namespace BusinessLayer
    Public Class Validations
#Region "Transaction"

        ''' <summary>
        ''' To check for numeric data.
        ''' </summary>
        ''' <param name="lstrEmailAddress"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function EmailAddressCheck(ByVal lstrEmailAddress As String) As Boolean
            Dim lstrPattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
            Dim emailAddressMatch As Match = Regex.Match(lstrEmailAddress, lstrPattern)
            If emailAddressMatch.Success Then
                EmailAddressCheck = True
            Else
                EmailAddressCheck = False
            End If
        End Function
        Public Shared Function CheckDate_ddmmmyyyy(ByRef val As String) As Boolean
            'Try
            If (IsDate(val) = False) Then Return False
            val = Convert.ToDateTime(val).ToString()
            Return True

        End Function
        Public Shared Function CheckNumeric(ByVal val As Char) As Boolean
            ' Try
            If (val = ".") Then Return True
            If (val = "-") Then Return True
            If Asc(val) = 8 Then Return True
            If Char.IsDigit(val) = False Then
                Return False
            Else
                Return True
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        Public Shared Function CheckPureNumeric(ByVal val As Char) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            If Char.IsDigit(val) = False Then
                Return False
            Else
                Return True
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        Public Shared Function CheckNumericString(ByVal val As String) As Boolean
            '  Try
            If Asc(val) = 8 Then Return True
            If IsNumeric(val) = False Then
                Return False
            Else
                Return True
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        Public Shared Function CheckNumericValue(ByVal val As Char) As Boolean
            ' Try
            If Asc(val) = 8 Then Return True
            If Char.IsDigit(val) = False Then
                Return False
            Else
                Return True
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        Public Shared Function CheckNumericWithDecimal(ByVal val As Char, ByVal CompleteWord As String) As Boolean
            ' Try

            If Asc(val) = 8 Then Return True
            If Microsoft.VisualBasic.InStr(CompleteWord, ".") <= 0 Then
                If (val = ".") Then Return True
            End If

            If Char.IsDigit(val) = False Then
                Return False
            Else
                Return True
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        Public Shared Function CheckForNumericForTwoDecimalPlaces(ByVal CompleteWord As String) As Boolean
            ' Try
            If Microsoft.VisualBasic.InStr(CompleteWord, ".") > 0 Then
                Dim dotLength As String = Right(Mid(CompleteWord, InStr(CompleteWord, ".")), Len(Mid(CompleteWord, InStr(CompleteWord, "."))) - 1)
                If Len(dotLength) > 2 Then
                    Return False
                End If
            End If
            Return True
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        Public Shared Function CheckNumber(ByVal val As String) As Boolean
            'Try
            If Asc(val) = 8 Then Return True

            If IsNumeric(val) = False Then
                Return False
            Else
                Return True
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function



        ''' <summary>
        ''' To check Years.
        ''' </summary>
        ''' <param name="val"></param>
        ''' <param name="val1"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckYear(ByVal val As Char, ByVal val1 As String) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            If Char.IsDigit(val) = False Then
                If Microsoft.VisualBasic.Len(val1) > 4 Then
                    Return False
                Else
                    Return True
                End If
            Else
                Return False
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for characters
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Function CheckCharacters(ByVal val As Char) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            If Char.IsWhiteSpace(val) Then Return True
            If Char.IsLetter(val) = False Then
                Return False
            Else
                Return True
            End If

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        '''  To check for Username and password fields
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckUserAndPassword(ByVal val As Char) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            If val = "_" Or val = "." Then Return True
            If Char.IsLetterOrDigit(val) Then
                Return True
            Else
                Return False
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for alphanumeric data
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Function CheckAlphaNumeric(ByVal val As Char) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            If Char.IsWhiteSpace(val) Then Return True
            If Char.IsLetterOrDigit(val) Then
                Return True
            Else
                Return False
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        Public Function CheckAlphaNumericWith_(ByVal val As Char) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            If Char.IsWhiteSpace(val) Then Return True
            If val = "_" Then Return True
            If val = "." Then Return True
            If Char.IsLetterOrDigit(val) Then
                Return True
            Else
                Return False
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check phone nos.
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckPhoneNos(ByVal val As Char) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            If Char.IsWhiteSpace(val) Then Return True
            If Char.IsDigit(val) Then
                Return True
            End If
            If val = "/" Or val = "-" Then
                Return True
            Else
                Return False
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for alphanumeric with slashes  data
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckAlphaNumericWithSlash(ByVal val As Char) As Boolean
            ' Try
            If Asc(val) = 8 Then Return True
            If Char.IsWhiteSpace(val) Then Return True
            If Char.IsLetterOrDigit(val) Then
                Return True
            End If
            If val = "/" Or val = "(" Or val = ")" Or val = "[" Or val = "]" Then
                Return True
            Else
                Return False
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for curreny format
        ''' </summary>
        ''' <param name="val"></param>
        ''' <param name="data"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Function CheckCurrencyFormat(ByVal val As Char, ByVal data As String) As Boolean
            'Try
            Dim position As Integer
            If Asc(val) = 8 Then Return True
            'If Asc(val) Then
            If Char.IsDigit(val) = False And Not val = "." Then Return False

            If val = "." Then
                If InStr(data, ".", CompareMethod.Text) > 0 Then Return False
            End If

            position = InStr(data, ".", CompareMethod.Text)
            If position > 0 Then
                If Len(data.Substring(position)) >= 2 Then Return False
            End If

            If Len(data) >= 7 And (position <= 0) Then
                If val = "." Then
                    Return True
                Else
                    Return False
                End If
            End If
            Return True
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for double.
        ''' </summary>
        ''' <param name="val"></param>
        ''' <param name="data"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckDouble(ByVal val As Char, ByVal data As String) As Boolean
            'Try
            If Asc(val) = 8 Then Return True
            'If Asc(val) Then
            If Char.IsDigit(val) = False And Not val = "." Then Return False

            If val = "." Then
                If InStr(data, ".", CompareMethod.Text) > 0 Then Return False
            End If
            Return True
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function


        ''' <summary>
        ''' To check for email address 
        ''' </summary>
        ''' <param name="Email"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>

        Public Shared Function CheckEmail(ByVal Email As String) As Boolean
            'Try
            Dim pattern As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
            Dim emailAddressMatch As Match = Regex.Match(Email, pattern)
            If emailAddressMatch.Success Then
                CheckEmail = True
            Else
                CheckEmail = False
            End If
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for special characters.
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckSpecialCharacters(ByVal val As Char) As Boolean
            ' Try
            If Asc(val) = 8 Then Return True
            If Char.IsWhiteSpace(val) Then Return True
            If Char.IsLetterOrDigit(val) = True Then Return True
            If val = "-" Or val = "/" Or val = "'" Or val = "(" Or val = ")" Or val = "[" Or val = "]" Or val = "_" Then
                Return True
            End If
            Return False

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        Public Shared Function CheckParenthesisCharacters(ByVal val As Char) As Boolean
            'Try
            If val = "(" Then
                Return False
            End If
            Return True
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for SQL special characters
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckSQLSpecialCharacters(ByVal val As String) As Boolean
            'Try

            If Asc(val) = 8 Then Return True
            'If Char.IsWhiteSpace(val) Then Return True
            'If Char.IsLetterOrDigit(val) = True Then Return True
            If val = "/" OrElse val = "\" OrElse val = "!" OrElse val = "`" OrElse val = "'" OrElse val = "222" Then
                Return True
            End If
            Return False

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for names
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckNames(ByVal val As Char) As Boolean
            ' Try
            If Asc(val) = 8 Then Return True
            If Char.IsWhiteSpace(val) Then Return True
            If Char.IsLetter(val) Then Return True
            If val = "'" Then Return True

            If val = "-" Then Return True
            Return False
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        ''' <summary>
        ''' To check quotes
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function CheckQuote(ByVal val As Char) As Boolean
            '   Try

            If val = "'" Then
                Return False
            Else
                Return True
            End If

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for numeric data
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function frmCheckNumeric(ByVal val As String) As Boolean
            'Try

            If Not IsNumeric(Trim(val)) Then Return False
            If InStr(val, ".") > 0 Then Return False
            Return True

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check for alphanumeric data
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shared Function frmCheckAlphaNumeric(ByVal val As String) As Boolean
            'Try
            Dim i As Int16
            Dim valid As String = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."

            val = Trim(val)
            For i = 0 To val.Length - 1
                If valid.IndexOf(val.Chars(i)) < 0 Then Return False
            Next
            Return True

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        Public Shared Function frmCheckAlphaNumericWith_(ByVal val As String) As Boolean
            'Try
            Dim i As Int16
            Dim valid As String = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_."

            val = Trim(val)
            For i = 0 To val.Length - 1
                If valid.IndexOf(val.Chars(i)) < 0 Then Return False
            Next
            Return True

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        ''' <summary>
        ''' To check Asset data.
        ''' </summary>
        ''' <param name="val"></param>
        ''' <returns>Returns Structure containing result sets and Exceptions if any</returns>
        ''' <remarks></remarks>
        ''' 

        Public Shadows Function CheckAssetData(ByVal val As String) As Boolean
            ' Try
            Dim i As Int16
            Dim valid As String = " 1234567890,"
            val = Trim(val)
            If Asc(val) = 8 Then Return True
            For i = 0 To val.Length - 1
                If valid.IndexOf(val.Chars(i)) < 0 Then Return False
            Next
            Return True
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

#End Region
    End Class
End Namespace